package com.monitor.core.aspect;

import com.monitor.core.annotation.ServiceMonitor;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.reflect.Method;

/**
 * @author hanhui.liu
 * @Description:
 * @date 2018/4/23 上午10:43
 */
@Aspect
public class ServiceAspect {
    private static final Logger logger = Logger.getLogger(ServiceAspect.class);

    public void doBefore(JoinPoint joinPoint) {
        logger.info("service aspect before");
    }

    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Method method = signature.getMethod();
        if (method.isAnnotationPresent(ServiceMonitor.class)) {
            ServiceMonitor serviceMonitor = method.getAnnotation(ServiceMonitor.class);
            System.out.println(serviceMonitor.name());
        }
        Object[] objects = method.getParameterTypes();
        for (int i = 0; i < objects.length; i++) {
            System.out.println(objects[i]);
        }
        pjp.getArgs();
        Object retval = pjp.proceed();
        return retval;
    }

    public void doAfter(JoinPoint joinPoint) {
    }

    public void doReturn(JoinPoint joinPoint, String retval) {

    }

    public void doThrowing(JoinPoint joinPoint, Exception e) {
    }
}
